import { wutu3d, Cesium } from "@/public/global/const";

var viewer;
//初始化地图

var sightline;
var init = false;

//当前页面业务相关
function initWork({ viewer: baseViewer }) {
    if (init) {
        return;
    }
    init = true;
    viewer = baseViewer;

    // viewer.scene.globe.depthTestAgainstTerrain = true;

    sightline = new wutu3d.analysi.Sightline(viewer);
}

function drawLine() {
    viewer.wutu.draw.deleteAll();
    viewer.wutu.draw.startDraw({
        type: "polyline",
        config: {
            maxPointNum: 2,
        },
        style: {
            color: "#55ff33",
            width: 3,
        },
        success: function(entity) {
            //绘制成功后回调
            var positions = entity.polyline.positions.getValue();
            sightline.add(positions[0], positions[1]);

            createPoint(positions[0], true);
            createPoint(positions[1], false);
            viewer.wutu.draw.deleteAll();
        },
    });
}

function drawCircle() {
    viewer.wutu.draw.deleteAll();
    viewer.wutu.draw.startDraw({
        type: "circle",
        style: {
            color: "#ffff00",
            opacity: 0.1,
            semiMinorAxis: 100,
            semiMajorAxis: 100,
            clampToGround: true,
        },
        success: function(entity) {
            //绘制成功后回调
            // viewer.scene.globe.depthTestAgainstTerrain = true;

            var center = entity.position.getValue();
            var radius = entity.ellipse.semiMajorAxis.getValue();
            //var height = Cesium.Cartographic.fromCartesian(center).height;

            var targetPoints = getArcPoints(center, radius);

            for (var i = 0; i < targetPoints.length; i++) {
                var targetPoint = targetPoints[i];

                targetPoint = wutu3d.point.updateHeightForClampToGround(viewer, targetPoint);

                sightline.add(center, targetPoint);
                // createPoint(targetPoint, false);
            }

            createPoint(center, true);
            viewer.wutu.draw.deleteAll();
        },
    });
}

function getArcPoints(position, radius, startAngle, endAngle) {
    startAngle = Cesium.defaultValue(startAngle, 0);
    endAngle = Cesium.defaultValue(endAngle, 360);

    var center = wutu3d.draw.util.cartesian2mercator(position);

    var x = null,
        y = null,
        pnts = [],
        angleDiff = endAngle - startAngle;
    angleDiff = angleDiff < 0 ? angleDiff + Math.PI * 2 : angleDiff;

    for (var i = 0; i <= angleDiff; i++) {
        var angle = startAngle + i;
        x = center[0] + radius * Math.cos(angle);
        y = center[1] + radius * Math.sin(angle);
        pnts.push([x, y]);
    }
    return wutu3d.draw.util.mercators2cartesians(pnts);
}

var arrPoint = [];

function clearAll() {
    sightline.clear();

    for (var i = 0, len = arrPoint.length; i < len; i++) {
        viewer.entities.remove(arrPoint[i]);
    }
    arrPoint = [];
}

function createPoint(position, isFirst) {
    var point = viewer.entities.add({
        position: position,
        point: {
            color: new Cesium.Color.fromCssColorString("#3388ff"),
            pixelSize: 6,
            outlineColor: new Cesium.Color.fromCssColorString("#ffffff"),
            outlineWidth: 2,
            scaleByDistance: new Cesium.NearFarScalar(1.5e2, 1.0, 8.0e6, 0.2),
        },
        label: {
            text: isFirst ? "观察位置" : "目标点",
            font: "normal small-caps normal 17px 楷体",
            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
            fillColor: Cesium.Color.AZURE,
            outlineColor: Cesium.Color.BLACK,
            outlineWidth: 2,
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            pixelOffset: new Cesium.Cartesian2(0, -20), //偏移量
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 2000000),
        },
    });
    arrPoint.push(point);
    return point;
}

export { initWork, drawLine, clearAll, drawCircle };
